package com.yunhe.spring.struts.controller;

import java.io.IOException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yunhe.spring.mybatis.model.Users;
import com.yunhe.spring.mybatis.service.IUsersService;

public class UsersAction extends ActionSupport implements ModelDriven<Object>{

	/**
	 * 用户控制类controller
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private IUsersService usersService;
	private Users users;
	
	

	public void setUsers(Users users) {
		this.users = users;
	}
	

	@Override
	public Object getModel() {
		if(users==null){
			users=new Users();
		}
		return users;
	}

	public Users getUsers() {
		return users;
	}


	//用户登录
	public void AdminLogin() throws IOException{
		//System.out.println(users.getUname());
		Users user=usersService.getOneUser(users);
		//System.out.println(user);
		JSONObject obj=new JSONObject();
		if(user!=null){
			ServletActionContext.getRequest().getSession().setAttribute("user", user);
			obj.put("message", "success");			
		}
		else{
			obj.put("message", "error");
		}
		ServletActionContext.getResponse().getWriter().print(obj);
	}


}
